<link rel="stylesheet" href="/static/Admin/css/bootstrap-select.min.css">
<script src="/static/Admin/js/bootstrap-select.min.js"></script>
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title" id="ntModalLabel">{:lang('add_/change_employee_profile')}</h4>
</div>
<div class="modal-body">
    <div class="row clearfix">
        <div class="col-md-12 column">
            <div class="alert alert-dismissable alert-info">
                <button type="button" class="close">×</button>
                <p></p>
            </div>
        </div>
    </div>
    <div class="row clearfix">
        <div class="col-md-12 column">
            <ul class="nav nav-tabs">
                <li class="active">
                    <a href="#edit_tab_base" data-toggle="tab">{:lang('basic_information')}</a>
                </li>
                <li>
                    <a href="#edit_tab_person" data-toggle="tab">{:lang('personal_information')}</a>
                </li>
            </ul>
        </div>
    </div>

    <form class="tab-content formbox">
        <div role="tabpanel" class="tab-pane active" id="edit_tab_base">
            <div class="row clearfix" id="edit_table_base">
                <div class="col-md-12 column">
                    <table class="table table-striped table-hover ntTable">
                        <tbody>
                        <tr>
                            <td class="col-md-2">{:lang('country')}<span class="required"></span></td>
                            <td>
                                <select class="form-control col-md-2" id="changeselect">
                                    {volist name="countrys" id="vo"}
                                    <option value="{$key}">{$vo}</option>
                                    {/volist}
                                </select>
                            </td>
                            <td>{:lang('default_language')}</td>
                            <td colspan="3">
                                <select name="language_id" class="form-control">
                                    <option value="">{:lang('please_select')}</option>
                                    {volist name="lang" id="vo"}
                                    {eq name="$key" value="$info.language_id|default=''"}
                                    <option value="{$key}" selected>{$vo}</option>
                                    {else/}
                                    <option value="{$key}">{$vo}</option>
                                    {/eq}
                                    {/volist}
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>{:lang('account')}<span class="required"></span></td>
                            <td>
                                <input type="text" class="form-control" placeholder="{:lang('login_account')}" name="name"
                                       value="{$info.name|default=''}">
                            </td>
                            <td>{:lang('nickname')}</td>
                            <td colspan="3">
                                <input type="text" class="form-control" placeholder="{:lang('nickname')}" name="nickname"
                                       value="{$info.nickname|default=''}">
                            </td>
                        </tr>
                        <tr>
                            <td>{:lang('department')}<span class="required"></span></td>
                            <td>
                                <div class="input-group">
                                    <select class="selectpicker" name="department_id" id="departments">

                                    </select>
                                </div>
                            </td>
                            <td>{:lang('job')}<span class="required"></span></td>
                            <td>
                                <div class="input-group">
                                    <select class="selectpicker select" id="positions" name="position_id">
                                    </select>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>{:lang('entry_time')}<span class="required"></span></td>
                            <td>
                                <div class="input-group date form_datetime"
                                     data-link-field="workdate">
                                    <input class="form-control" name="work_day" type="text"
                                           value="{$info.work_day|default=$work_day}">
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"
                                                                          style="margin:0"></span></span>
                                </div>
                                <input type="hidden" id="workdate" value=""/>
                            </td>
                            {if ($str eq 'personneledit')}
                            <td class="col-md-2">{:lang('resignation_date')}<span class="required"></span></td>
                            <td>
                                <div class="input-group date form_datetime" 
                                     data-link-field="leavedate">
                                    <input class="form-control" name="leave_day" type="text"
                                           value="{$info.leave_day|default=''}">
                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"
                                                                          style="margin:0"></span></span>
                                </div>
                                <input type="hidden" id="leavedate" value=""/>
                            </td>
                            {/if}
                        </tr>
                        <tr>
                            <td>{:lang('basic_salary')}<span class="required"></span></td>
                            <td>
                                <input type="number" class="form-control" name="wage" placeholder="{:lang('basic_salary')}"
                                       value="{$info.wage|default=''}"
                                       style="text-align:right">
                            </td>
                            <td>{:lang('status')}<span class="required"></span></td>
                            <td>
                                {volist name="status" id="vo"}
                                <label class="radio-inline">
                                    {eq name="$key" value="$info.status|default='1'"}
                                    <input type="radio" name="status" value="{$key}" checked="checked">{$vo}{else/}
                                    <input type="radio" name="status" value="{$key}">{$vo}
                                    {/eq}
                                </label>
                                {/volist}
                            </td>
                        </tr>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
        <div role="tabpanel" class="tab-pane" id="edit_tab_person">
            <div class="row clearfix" id="edit_table_person">
                <div class="col-md-12 column">
                    <table class="table table-striped table-hover ntTable">
                        <tbody>
                        <tr>
                            <td>{:lang('name')}<span class="required"></span></td>
                            <td>
                                <div >
                                    <input type="text" class="form-control" name="firstname" placeholder="姓"
                                           value="{$info.firstname|default=''}">
                                </div>
                                </td>
                                <td></td>
                                <td>
                                <div >
                                    <input type="text" class="form-control" name="lastname" placeholder="名"
                                           value="{$info.lastname|default=''}">
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>{:lang('brithday')}<span class="required"></span></td>
                            <td>
                                <div class="input-group date form_datetime" data-link-field="birthday">
                                    {if ($str eq 'personneladd')}
                                    <input class="form-control" type="text" name="birthday"
                                           value="{$birth}">
                                    {else/}
                                    <input class="form-control" type="text" name="birthday"
                                           value="{$info.birthday}">
                                    {/if}

                                    <span class="input-group-addon"><span class="glyphicon glyphicon-calendar"
                                                                          style="margin:0"></span></span>
                                </div>
                                <input type="hidden" id="birthday"/>
                            </td>

                            <td>{:lang('education')}</td>
                            <td>
                                <div class="input-group">
                                    <select class="selectpicker select" name="education" lay-verify="" lay-search>
                                        <option value="">{:lang('please_select')}</option>
                                        {volist name="education" id="vo"}
                                        {eq name="$key" value="$info.education|default=''"}
                                        <option value="{$key}" selected>{$vo}</option>
                                        {else/}
                                        <option value="{$key}">{$vo}</option>
                                        {/eq}
                                        {/volist}
                                    </select>
                                </div>
                            </td>
                        </tr>
                        <tr>
                            <td>{:lang('gender')}<span class="required"></span></td>
                            <td>
                                {volist name="sex" id="vo"}
                                <label class="radio-inline">
                                    {eq name="$key" value="$info.sex|default='1'"}
                                    <input type="radio" name="sex" value="{$key}" checked="checked">{$vo}{else/}
                                    <input type="radio" name="sex" value="{$key}">{$vo}
                                    {/eq}
                                </label>
                                {/volist}
                            </td>

                            <td>{:lang('married_yes/no')}<span class="required"></span></td>
                            <td>
                                {volist name="marry" id="vo"}
                                <label class="radio-inline">
                                    {eq name="$key" value="$info.marry|default='1'"}
                                    <input type="radio" name="marry" value="{$key}" checked="checked">{$vo}{else/}
                                    <input type="radio" name="marry" value="{$key}">{$vo}
                                    {/eq}
                                </label>
                                {/volist}
                            </td>
                        </tr>
                        </tbody>
                    </table>

                </div>
            </div>
        </div>
        <input type="hidden" id="act" name="act" value="{$str}">
        <input type="hidden" name="id" id="id" value="{$info.id|default=''}">
        <div style="display: none">
            <span id="department_id">{$info.department_id|default=''}</span>
            <span id="position_id">{$info.position_id|default=''}</span>
            <span id="decountry_id">{$info.decountry_id|default=''}</span>
            <span id="pocountry_id">{$info.pocountry_id|default=''}</span>
        </div>
    </form>
</div>
<div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal"><span
            class="glyphicon glyphicon-remove"></span>{:lang('cancel')}
    </button>
    <button type="button" id="quedin" class="btn btn-primary"
            style="padding-left:24px;padding-right:24px"><span
            class="glyphicon glyphicon-floppy-disk"></span>{:lang('save')}
    </button>
</div>
<script>
    $(function () {
        var position_id = $('#position_id').text();//职位id
        var department_id = $('#department_id').text();//部门id
        var pocountry_id=$('#pocountry_id').text();
        if(!pocountry_id){
            pocountry_id=0
        }
        var decountry_id=$('#decountry_id').text();
        if(!decountry_id){
            decountry_id=0
        }
        var addbm = true;
        var action = $('#act').val();
        var lang = $('#lang').text();
        var language = '';
        if (lang == 'pt-br') {
            language = 'pt-BR';
            langsql = 'pt';
            format = 'mm-dd-yyyy';
        } else if (lang == 'en-us') {
            language = 'en';
            langsql = 'en';
            format = 'mm-dd-yyyy';
        } else {
            language = 'zh-CN';
            langsql = 'zh';
            format = 'yyyy-mm-dd'
        }
        //默认请求
        getframe(pocountry_id, 'position');
        getframe(decountry_id, 'department');
        $('.selectpicker').selectpicker({});
        //数据的提交
        $('#quedin').click(subfn)
        function subfn() {
            if (addbm) {
                $.ajax({
                    url: '/Admin/Organization/' + action,
                    type: 'post',
                    data: $(".formbox").serialize(),
                    dataType: 'json',
                    success: function (res) {
                        if (res.status != 1) {
                            change_mes_type('.modal-body .alert',res.status)
                            $('.modal-body .alert').show()
                            $('.modal-body .alert p').html('*' + res.msg + '<br>')
                            //toastr.warning(res.msg,);
                        } else {
                            change_mes_type('.alert',res.status)
                            addbm = false
                            if (action == 'personneladd') {
                                $('.alert').show()
                                $('.alert p').html('*' + '添加成功：账号' + res.name + '密码' + res.passwd + '<br>')
                                $('#worker_editModal').modal('toggle')
                            } else {
                                $('.alert').show()
                                $('.alert p').html('*' + res.msg + '<br>')
                                $('#worker_editModal').modal('toggle')
                            }
                        }
                    }
                });
            }
        }

        $('#changeselect').change(function () {
            var coun_id = $('#changeselect').val();
            //获取对应国家下的职位
            getframe(coun_id, 'position');
            //获取对应国家下的部门
            getframe(coun_id, 'department');
        });
        function getframe(coun_id, tabel) {
            $.ajax({
                url: '/admin/api/postionfrmaes',
                type: 'post',
                data: {
                    table: tabel,
                    lang: langsql,
                    country_id: coun_id
                },
                dataType: 'json',
                success: function (res) {
                    if (tabel == 'department') {
                        //部门的渲染
                        modelde(res, 1);
                    } else {
                        //职位渲染
                        modelde(res, 2);
                    }
                }
            });
        }

        function modelde(res, code) {
            if (code == 1) {
                //将部门追加到页面
                if (res.length) {
                    var html = "<option value=''>{$Think.lang.please_select}</option>"
                    for (var i = 0; i < res.length; i++) {
                        var strs = '';
                        for (var j = 0; j < res[i].level; j++) {
                            strs += '&nbsp;&nbsp;&nbsp;&nbsp;'
                        }
                        var id = res[i].id;
                        if (id == department_id) {
                            html += '<option selected value="' + res[i].id + '">' + strs + res[i].langname + '</option>';
                        } else {
                            html += '<option value="' + res[i].id + '">' + strs + res[i].langname + '</option>';
                        }
                    }
                    $('#departments').html(html);
                    $('#departments').selectpicker('refresh');
                    $('#departments').selectpicker('render');
                }
            } else {
                //职位的渲染
                if (res.length) {
                    var html = "<option value=''>{$Think.lang.please_select}</option>"
                    for (var i = 0; i < res.length; i++) {
                        var strs = '';
                        for (var j = 0; j < res[i].level; j++) {
                            strs += '&nbsp;&nbsp;&nbsp;&nbsp;'
                        }
                        var id = res[i].id;
                        if (id == position_id) {
                            html += '<option selected value="' + res[i].id + '">' + strs + res[i].langname + '</option>';
                        } else {
                            html += '<option value="' + res[i].id + '">' + strs + res[i].langname + '</option>';
                        }
                    }
                    $('#positions').html(html);
                    $('#positions').selectpicker('refresh');
                    $('#positions').selectpicker('render');
                }
            }
        }
        $('.form_datetime').datetimepicker({
            language: language, // en ,zh-CN, pt-BR
            format: format,
            minView: 'month',
            weekStart: 0, //一周从哪一天开始
            todayBtn: 1, //底部会出现一个今天
            autoclose: true,//自动关闭
            todayHighlight: 1,//高亮今天日期
            startView: 2,//显示的视图
            forceParse: 0,//当选择器关闭的时候，是否强制解析输入框中的值
            showMeridian: 1
        });
        $('.form_datetime').on('changeDate', function (ev) {
        }).on('hide', function (event) {
            event.preventDefault();
            event.stopPropagation();
        });
    })
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            $('form').each(function () {
                event.preventDefault();
            });
            subfn()
        }
    });
</script>